#include "Math.h"
#include <math.h>
bool Math::isInitSinCosTable = false;
double Math::mSinTable[360] = { 0 };
double Math::mCosTable[360] = { 0 };
#define PI 3.1415926f
Math::Math()
{
	initSinCosTable();
}


Math::~Math()
{
}

void Math::initSinCosTable()
{
	for (int ang = 0; ang < 360;ang++)
	{
		double theta = (double)ang * PI / (double)180;
		mCosTable[ang] = cos(theta);
		mSinTable[ang] = sin(theta);
	}
	isInitSinCosTable = true;
}

double Math::sin(double theta_du)
{
	if(!isInitSinCosTable){
		initSinCosTable();
	}

	theta_du = fmod(theta_du, 360);
	if(theta_du <0){
		theta_du += 360.0f;
	}

	int theta_int = int(theta_du);
	double theta_frac = theta_du - theta_int;

	double result = mSinTable[theta_int] +
		theta_frac*(mSinTable[theta_int + 1] -
		mSinTable[theta_int]);
	return result;

}
double Math::cos(double theta_du)
{
	if (!isInitSinCosTable){
		initSinCosTable();
	}
	theta_du = fmod(theta_du, 360);
	if (theta_du < 0){
		theta_du += 360.0f;
	}

	int theta_int = int(theta_du);
	double theta_frac = theta_du - theta_int;

	double result = mCosTable[theta_int] +
		theta_frac*(mCosTable[theta_int + 1] -
		mCosTable[theta_int]);
	return result;
}